<!--斑马柱状图-->

<template>
  <div class="w-h-full">
    <!--组件遮罩层，用于在设计器中显示-->
    <div v-if="shade" class="shade"></div>

    <div v-resize="resizeHandle" style="width: 100%; height: 100%;">
      <div :id="'chart-'+component.id+'-'+chartDomNum" class="w-h-full"></div>
    </div>
  </div>
</template>

<script>

  import FuncCompMixin from '@/mixin/FuncCompMixin'
  import ChartCompMixin from '@/mixin/ChartCompMixin'
  import PnDesigner from '@/utils/PnDesigner'

  const _this = {
    name: 'ZebraBarChart', // 此名称必须填写，且必须与组件文件名相同
    mixins: [FuncCompMixin, ChartCompMixin],
    attr: {
      version: '2.3',
      configDataTemp: Object.assign({

        // 组件相关配置属性，Echarts图表组件的相关配置属性，必须在chartOption对象下定义
        chartOption: {
          title: PnDesigner.buildTitleConfigData(),
          tooltip: PnDesigner.buildTooltipConfigData({
            axisPointer: {
              type: 'shadow'
            }
          }),
          grid: PnDesigner.buildGridConfigData({
            left: '10%',
            bottom: '10%'
          }),
          xAxis: PnDesigner.buildXAxisConfigData(),
          yAxis: PnDesigner.buildYAxisConfigData({
            name: '数量',
            show: true
          }),
          series: {
            symbolSize: ['80%', 5],
            symbolMargin: 2,

            label: {
              show: false,
              position: 'inside',
              color: '#fff',
              fontSize: 12
            },
            itemStyle: {
              color: '',
            },
          }
        }

      }, PnDesigner.buildFuncCompDatasourceField({ // 如果组件需要配置数据源，则需要调用此函数构建数据源字段对象
        ds_resultObjTemplate: [
          {
            field: 'name',
            remark: '类目'
          },
          {
            field: 'value',
            remark: '值'
          },
        ],
        ds_resultObj: [
          {
            name: '广西',
            value: 250,
          },
          {
            name: '广东',
            value: 443,
          },
          {
            name: '上海',
            value: 594,
          },
          {
            name: '北京',
            value: 877
          },
        ],
        ds_useFilter: true, // 是否使用过滤器，默认不使用
        ds_useTimer: true, // 是否开启定时器配置，默认不开启
      }), PnDesigner.buildFuncCompGlobalField(), PnDesigner.buildEchartCompCustomOption(), PnDesigner.buildFuncCompInteractionConfiguration([
        PnDesigner.buildFuncCompInteractionStreamItem('itemClick', [
          PnDesigner.buildFuncCompInteractionFieldsMapItem('name', '', '类目'),
          PnDesigner.buildFuncCompInteractionFieldsMapItem('value', '', '值')
        ])
      ]))
    },
    data() {
      return {}
    },
    mounted() {
      this.init();

      this.setTimer(() => { //定时器，如果当前组件不使用定时器，可以将此段代码删除
        this.init()
      })
    },
    methods: {
      init() {
        this.initDatasource(() => {
          this.chart.setOption(this.chartOption);
          this.runCustomGlobalJsCode();
          this.runDataItemClickJsCode(this.launchInteractionStream)
        }, (result) => {
          this.chart.setOption(this.chartOption);
          this.runCustomGlobalJsCode();
          this.runDataItemClickJsCode(this.launchInteractionStream)
        });
      },

      launchInteractionStream (params) {
        this.$PnDesigner.launchInteractionStream(this.component.compConfigData, 'itemClick', params.data)
      }
    },
    computed: {
      chartOption () {

        let option = {
          title: PnDesigner.buildTitleChartOption(this.component.compConfigData.chartOption.title),
          tooltip: PnDesigner.buildTooltipChartOption(this.component.compConfigData.chartOption.tooltip),
          grid: PnDesigner.buildGridChartOption(this.component.compConfigData.chartOption.grid),
          xAxis: PnDesigner.buildXAxisChartOption(this.component.compConfigData.chartOption.xAxis),
          yAxis: PnDesigner.buildYAxisChartOption(this.component.compConfigData.chartOption.yAxis),
          series: [
            {
              type: 'pictorialBar',
              symbol: 'fixed',
              symbolRepeat: 'repeat',
              symbolSize: [
                this.component.compConfigData.chartOption.series.symbolSize[0],
                this.component.compConfigData.chartOption.series.symbolSize[1]
              ],
              symbolMargin: this.component.compConfigData.chartOption.series.symbolMargin,

              label: {
                show: this.component.compConfigData.chartOption.series.label.show,
                position: this.component.compConfigData.chartOption.series.label.position,
                color: this.component.compConfigData.chartOption.series.label.color,
                fontSize: this.component.compConfigData.chartOption.series.label.fontSize
              },

              itemStyle: {
                color: this.component.compConfigData.chartOption.series.itemStyle.color ?
                  this.component.compConfigData.chartOption.series.itemStyle.color : this.currentEchartThemeColors[2],
              },

              data: []
            }
          ]
        };

        option.grid.containLabel = false

        this.runCustomFilter(this.component.compConfigData.ds_resultObj).forEach(item => {
          option.xAxis.data.push(item.name);
          option.series[0].data.push(item)
        });

        // 下面这一段代码用于合并组件的扩展配置，如果组件不需要扩展配置，可以删掉
        option = this.$PnDesigner.mergeChartCustomOption(option, this.component.compConfigData.customOption);

        return option;
      }
    }
  };

  export default _this
</script>

<style scoped>

</style>
